/*
 * Drill.java
 *
 * Created on 2007. mC!rcius 23., 23:58
 *
 */

package laborhazi;

import java.util.Random;

/**
 * Drill - a tool that digs a hole
 * @author nyuszik
 */
public class Drill extends Tool{
	private boolean forward = true;
    private int power = 6;
    private Field field;
    private int faszom;
    //private Random generator;
    /** Creates a new instance of Drill
     * @param f the Field
     */
    public Drill() {
    }
    public Drill(Field f) {
        field = f;
        Random generator = new Random();
        faszom = generator.nextInt();
    }
    public String toString() {
    	return ("Drill");
    }
    /** 
     * Using the drill will destroy a nearby stone (if it has enough power left)
     * @param lemming the lemming who uses the drill
     */
    public void use(Lemming lemming){
        if (checkPower() == true && !lemming.isFalling()) {
        	power--;
            int[] position = new int[2];
            int[] pos = lemming.getPosition();
            position[0] = pos[0];
            position[1] = pos[1];
            int dir = lemming.getDir();
            int direction;
            if(dir == 0) {
            	direction = -1;
            } else direction = 1;
            if (forward) {
            	position[0] = position[0] + direction;
            } else {
            	position[1] = position[1] - 1;
            }
            if (field.check(position) instanceof Stone) {
            	field.remove(field.check(position), position);
            	lemming.setDrilling(true);
            }
            forward = !forward;
        } else if (!checkPower()) {
            erase(lemming);
        }
    }
    /** 
     * Checks if the drill has enough power left
     */
    private boolean checkPower() {
        if (power > 0) return true;
        else return false;
    }
    /** 
     * Removes the drill
     */
    private void erase(Lemming lemming) {
    	System.out.println("ELFOGYOTT A DRILL!!!!!!!!!   " + power + " " + faszom);
    	lemming.removeTool(4);
    }
}
